﻿using System;
using System.Linq;
using System.Collections.Generic;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model.Dto;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.BLL
{
    public partial class McCashWithdrawApplicationBLL : BaseBLL<McCashWithdrawApplicationBLL>
    {
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(McCashWithdrawApplication model)
        {
            return McCashWithdrawApplicationDAL.Instance.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(McCashWithdrawApplication model)
        {
            return McCashWithdrawApplicationDAL.Instance.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return McCashWithdrawApplicationDAL.Instance.Delete(Id);
        }
        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return McCashWithdrawApplicationDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public McCashWithdrawApplication GetModel(int Id)
        {
            var ds = McCashWithdrawApplicationDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<McCashWithdrawApplication>(ds).ToList()[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<McCashWithdrawApplication> GetModelList(string strWhere = null)
        {
            var ds = McCashWithdrawApplicationDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<McCashWithdrawApplication>(ds).ToList();
        }
        #endregion

        #region 扩展方法

        /// <summary>
        /// 获取店铺历史总提现金额
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public List<McCashWithdrawApplication> GetTotalWithdrawCashByShopId(int shopId)
        {
            return McCashWithdrawApplicationDAL.Instance.GetTotalWithdrawCashByShopId(shopId);
        }


        /// <summary>
        /// 获得分页数据列表
        /// </summary>
        public TableDataViewDto<ViewMcCashWithdrawApplicationDto> GetPagingList(int draw, int pageIndex, int pageSize,
            string searchKeyWord, string colOrder, int dealStatus, int shopId, bool showOptBtn = false)
        {
            try
            {
                var totalCount = 0;
                var list =
                    McCashWithdrawApplicationDAL.Instance.GetPagingList(pageIndex, pageSize, searchKeyWord, colOrder,
                        dealStatus, shopId, ref totalCount);
                list.ForEach(a =>
                {
                    a.DealStatusStr = a.DealStatus == 0 ? "待处理" : a.DealStatus == 1 ? "处理成功" : a.DealStatus == 2 ? "处理失败" : "";
                    a.ApplyTimeStr = a.ApplyTime == null
                        ? string.Empty
                        : ((DateTime)a.ApplyTime).ToString("yyyy-MM-dd HH:mm:ss");
                    a.PayTimeStr = a.PayTime == null
                        ? string.Empty
                        : ((DateTime)a.PayTime).ToString("yyyy-MM-dd HH:mm:ss");

                    if (showOptBtn)
                    {
                        if (a.DealStatus == (int)CommonEnum.McWithdrawDealStatus.Pending)
                            a.Action = "<a class='btn btn-success' href='javascript:;' onclick=Operation(" + a.Id +
                                        ",0);>处理确认</a>   <a class='btn btn-danger' href='javascript:;' onclick=Operation(" +
                                        a.Id + ",1);>处理失败</a>";
                    }
                });
                var returnDataList = new TableDataViewDto<ViewMcCashWithdrawApplicationDto>
                {
                    data = list,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };

                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }

        #endregion
    }
}